package com.ck.ck_back.service.impl;

import com.ck.ck_back.dao.LikeCommentMapper;
import com.ck.ck_back.pojo.LikeComment;
import com.ck.ck_back.service.likecommentservice;
import com.ck.ck_back.utils.RedisKeyUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class likecommentserviceImpl implements likecommentservice {
    @Resource
    private LikeCommentMapper likeCommentMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public void saveLiked2Redis(int likedCommentId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedCommentId,likedPostId);
        String value =RedisKeyUtils.getLikedvalue(1,new Date().getTime());
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_Comment_LIKED,key,value);
    }

    @Override
    public void unlikeFromRedis(int likedCommentId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedCommentId,likedPostId);
        String value =RedisKeyUtils.getLikedvalue(0,new Date().getTime());
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_Comment_LIKED,key,value);
    }


    @Override
    public List<LikeComment> getLikedDataFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_Comment_LIKED, ScanOptions.NONE);
        List<LikeComment> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object,Object> entry = cursor.next();
            String key = (String) entry.getKey();
            String[] split = key.split("::");
            int likedCommentId = Integer.parseInt(split[0]);
            String likedPostId = split[1];

            String value = (String) entry.getValue();
            String[] splitt = value.split("::");
            Integer state = Integer.parseInt(splitt[0]);
            Long updateTime = Long.valueOf(splitt[1]);

            //组装成 UserLike 对象
            LikeComment likeComment = new LikeComment();
            likeComment.setLikedCommentId(likedCommentId);
            likeComment.setLikedPostId(likedPostId);
            likeComment.setState(state);
            likeComment.setUpdateTime(updateTime);
            list.add(likeComment);
            //存到 list 后从 Redis 中删除
            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_Comment_LIKED, key);
        }
        return list;
    }


    @Transactional
    @Override
    public void deal_withdata(List<LikeComment> likeComments) {
        System.out.println("进入了方法");
//        判断mysql中是否有该评论，有则改，无则加
        for (LikeComment likecomment :likeComments ) {
            LikeComment likeComment = likeCommentMapper.selectBytwo_id(likecomment.getLikedCommentId(),likecomment.getLikedPostId());

            if (likeComment == null){
                likeCommentMapper.insertSelective(likecomment);
            }else {
                likeComment.setState(likecomment.getState());
                likeComment.setUpdateTime(likecomment.getUpdateTime());
                likeCommentMapper.updateByPrimaryKey(likeComment);
            }
        }
    }

    //    给前端返回是否点赞的信息,以redis内信息为主，先检查信息是否正确，再看redis中是否有信息
//    没有再看数据库，在没有报错,404参数错误，0/1为状态
    @Override
    public int getstate(int likedCommentId, String likedPostId) {
        if(!String.valueOf(likedCommentId).equals("")&&likedPostId!=null){
            String key = RedisKeyUtils.getLikedKey(likedCommentId,likedPostId);
            redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_Comment_LIKED,key);
            String obj = (String) redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_Comment_LIKED,key);
            System.out.println(obj);
            if (obj!=null&&!obj.equals("")){
                String[] split = obj.split("::");
                Integer state = Integer.parseInt(split[0]);
                return state;
            }else {
                LikeComment likeComment = likeCommentMapper.selectBytwo_id(likedCommentId,likedPostId);
                if (likeComment!=null){
                    return likeComment.getState();
                }else {
                    return 0;
                }
            }
        }else {return 404;}
    }


}
